#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int N = 2010;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int q;
    cin >> q;
    while(q--){
        string s;
        cin >> s;
        if(s.size() != n) {
            cout << "NO\n";
            continue;
        }
        map<char, int> mp1;
        map<int, char> mp2;
        int f = 0;
        for(int i = 0; i < n; i++) {
            if(!mp1.count(s[i]) && !mp2.count(a[i])){
                mp1[s[i]] = a[i];
                mp2[a[i]] = s[i];
            }else{
                if(mp1[s[i]] != a[i] || mp2[a[i]] != s[i]) {
                    f = 1;
                    cout << "NO\n";
                    break;
                }
            }
        }
        if(!f) cout << "YES\n";
    }
}

int main()
{
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}